<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col>
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          @click="fastHandle"
          v-hasPermi="['data:deviceData:manager']"
        >{{$t('deviceData.popup.handleAall')}}</el-button>
      </el-col>
    </el-row>
    <el-table :data="eventDataList" >
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column :label="$t('deviceData.popup.device')" align="center" prop="deviceName" />
      <el-table-column :label="$t('deviceData.popup.sensor')" align="center" prop="sensorName" />
      <el-table-column :label="$t('deviceData.popup.channelData')" align="center" prop="channelData">
        <template slot-scope="scope">
          {{scope.row.channelData+scope.row.unit}}
        </template>        
      </el-table-column>
      <el-table-column :label="$t('deviceData.popup.eventName')" align="center" prop="eventName" />
      <el-table-column :label="$t('deviceData.popup.associatedData')" align="center" prop="associatedData" >
        <template slot-scope="scope">
          {{seriesList.filter(o=> o.flag == scope.row.associatedData).map( o => o.name).join()}}
        </template>
      </el-table-column>
      <el-table-column :label="$t('deviceData.popup.dataValue')" align="center" prop="associatedDataValue" />
      <!-- <el-table-column label="是否处理" align="center" prop="triggerModel">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sensor_trigger_model" :value="scope.row.triggerModel"/>
        </template>
      </el-table-column> -->
      <el-table-column :label="$t('deviceData.popup.alarmLevel')" align="center" prop="alarmModel">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.alarm_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column :label="$t('deviceData.popup.alarmContent')" align="center" prop="alarmContent" />
      <el-table-column :label="$t('deviceData.popup.startTime')" align="center" prop="deviceTime" width="180" />
      <el-table-column :label="$t('deviceData.popup.endTime')" align="center" prop="finishTime" width="180" />
      <el-table-column :label="$t('deviceData.popup.operation')" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            v-if="scope.row.isHandle==0"
            @click="hancleClick(scope.row)"
          >{{ $t('deviceData.popup.handling') }}</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 处理报警 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form"  label-width="120px">
        <el-form-item :label="$t('deviceData.popup.eventName')" prop="eventName" v-if="form.id">
          <el-input v-model="form.eventName" :placeholder="$t('deviceData.popup.eventName')" disabled/>
        </el-form-item>
        <el-form-item :label="$t('deviceData.popup.channelData')" prop="expression" v-if="form.id">
          <el-input v-model="form.channelData" :placeholder="$t('deviceData.popup.channelData')" disabled />
        </el-form-item>
        <el-form-item :label="$t('deviceData.popup.remark')" prop="remark">
          <el-input v-model="form.remark" type="textarea" rows="5" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitHandle">{{$t('common.confirm')}}</el-button>
        <el-button @click="cancel">{{$t('common.cancel')}}</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { listEventData } from "@/api/device/device";
import { handleEventData ,fastHandle} from "@/api/data/eventData";
import code from '../cmdData/code'
import { seriesIoOptions} from "@/api/device/seriesIo";
export default {
  props:{
    deviceId:'',
    sensorId:'',
    deviceTypeId:'',
  },
  dicts: ['alarm_status'],
  components:{'v-code':code},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum:1,
        pageSize:10
      },
      // 表单参数
      form: {},
      //设备报警列表
      eventDataList:[],
      seriesList:[],
    };
  },
  created() {
    if(this.deviceId){
      this.queryParams.deviceId = this.deviceId;
      this.queryParams.sensorId = this.sensorId||'';
      this.getSeriesIoOptions()
      this.getList();
    }
  },
  methods: {
    /** 查询设备命令执行日志列表 */
    getList() {
      this.loading = true;
      listEventData(this.queryParams).then(response => {
        this.eventDataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /**获取控制按钮**/
    getSeriesIoOptions(){
        let params = {
          sensorId:this.sensorId,
          deviceTypeId:this.deviceTypeId,
          control:0
        }
        seriesIoOptions(params).then(res=>{
          this.seriesList = res.data;
        })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    hancleClick(row){
      this.title = row.eventName;
      this.reset();
      this.form = row ;
      this.open = true ;
    },
    fastHandle(){
      this.title = this.$t('deviceData.popup.handling');
      this.reset();
      this.form.deviceId = this.deviceId;
      this.form.sensorId = this.sensorId;
      this.open = true ;
    },
    submitHandle(){
      if(this.form.id){
        handleEventData(this.form).then(res => {
          this.$modal.msgSuccess("处理成功!");
          this.open = false;
          this.getList();
        })
      }else{
        this.submitFastHandle()
      }
    },
    submitFastHandle(){
      fastHandle(this.form).then(res => {
        this.$modal.msgSuccess("处理成功!");
        this.open = false;
        this.getList();
      })
    },
    // 表单重置
    reset() {
      this.form = {
        id:null,
        deviceId:null,
        sensorId:null,
        sensorName:null,
        deviceName:null,
        channelData:null,
        remark:null,
        expression:null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加设备命令执行日志";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getCmdData(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改设备命令执行日志";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCmdData(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCmdData(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除设备命令执行日志编号为"' + ids + '"的数据项？').then(function() {
        return delCmdData(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('data/cmdData/export', {
        ...this.queryParams
      }, `cmdData_${new Date().getTime()}.xlsx`)
    },
    /** json展示框 **/
    viewJSON(res){
      this.jsonTitle = res.topic||''
      if(res.message.length>0){
        if(res.message[0]==='{'&&res.message[res.message.length-1]==='}'){
          this.json = JSON.stringify(JSON.parse(res.message),null,'\t');
        }else{
          this.json = res.message
        }
      }
      this.jsonPopup = true;
    },
    viewReplyJSON(res){
      this.jsonTitle = res.replyTopic||''
      if(res.replyMessaeg.length>0){
        if(res.replyMessaeg[0]==='{'&&res.replyMessaeg[res.replyMessaeg.length-1]==='}'){
          this.json = JSON.stringify(JSON.parse(res.replyMessaeg),null,'\t');
        }else{
          this.json = res.replyMessaeg
        }
      }
      this.jsonPopup = true;
    }
  },
  watch:{
    sensorId(v){
      if(v){
        this.queryParams.deviceId = this.deviceId;
        this.queryParams.sensorId = this.sensorId||'';
        this.getList();
      }
    }
  }
};
</script>
